/**
 * 
 */


var Organzation = {
	init: function(){
		Organzation.initEvent();
		Organzation.initCombo();
		Organzation.initAreaName();
		this.bind();
	},
	initEvent: function(){
		$('.date-alarm-ul li').bind('click',function(i){
			if($(this).index()==0){
				$('.date-alarm').css('left','0px');
				$("#baseInfoDiv").show();
				$("#otherInfoDiv").hide();
			}else{
				$('.line').css('left','100px');
				$("#baseInfoDiv").hide();
				$("#otherInfoDiv").show();
			}
		});
	},
	
	bind : function(){
		$('body').on('click','#addTerminalPanel',function(){
			
			if ($('#workUnitId').val() == '') {
				showVelidateModal('请选择企业');
				return;
			}
			$("#terminal_UnitID").val($('#workUnitId').val());
			$("#terminal_UnitName").val($('#workUnitId').combotree("getText"));
			$("#terminal_SimNo").focus();
			Organzation.initPanel();
			$('.TerminalPanel').show();
			
		});
		
		$('body').on('click','.TerminalPanel .close',function(){
			$('.TerminalPanel').hide();
		});
		
		$('body').on('click','#ClearTerminal',function(){
			$("#terminal_SimNo").val('');
			$("#terminal_ComNo").val('');
			$("#terminal_TerminalTypeID").combobox("setValue","");
		});
		
		$("#addTerminal").click(function(){
			Organzation.addTerminal()
		});
		
	},
	
	
	

	initCombo: function(){
		//企业名称
		var unitTool = $('#workUnitId').combotree({
			url : appCtx + "base/PubOrganzation/queryFullTree.html",
			valueField : 'id',
			textField : 'text',
			editable : true,
			onSelect : function(data) {
				$('#parentid').val(data.id);
/*				$('#workUnitName').val(data.text);*/
			}, //全部折叠
			onLoadSuccess : function(node, data) {
				$('#workUnitId').combotree('tree').tree("collapseAll");
				if(voname==null||voname==''){
					voname='请选择隶属单位'; 
				}
				$('#workUnitId').combotree('setText',voname);
			}
		});
		//查询 
		unitTool.combogrid('textbox').keyup(function(event) {
			var val = $.trim($(this).val());
			if(val != ''){
				$('#workUnitId').combotree('tree').tree("search", val);
			}
		});
	},
	
	initAreaName:function(){
		$('#area_id').combotree({
			url:appCtx + "/system/common/queryAreaTree.html",
			valueField: 'id',
            textField: 'text',
            method: 'post',
            editable : false,
            collapseAll : true,           
            loadFilter: function(data){
            	 data.unshift({ id: '', text: '请选择所属区域', selected: "selected" });
                 return data;
    		},
    		onLoadSuccess: function (data) {
    			$('#area_id').combotree('tree').tree("collapseAll");
    			if(areaId!=null||areaId!=''){
        			$('#area_id').combotree('setValue',areaId);
    			}
				if(areaName==null||areaName==''){
					areaName='请选择所属区域'; 
				}
    			$("#area_id").combotree('setText',areaName);
				$('#area_name').val(areaName);
         	},
         	onSelect : function(data) {
    			$("#area_name").val(data.text);
			}, 

		});
	},
		
	saveOrganzation:function(){
		if(!checkparam()){
			return;
		}
		if(action=='update'){
			// 保存运政机构
			$.ajax({
				type : 'post',
				url : appCtx + "base/PubOrganzation/updatePubOrganzation.html",
				data : $("#vehicleForm").serialize(),
				cache : false,
				dataType : "JSON",
				success : function(data) {
					if (data.code == "0") {
						global.ajaxLoading.pop('操作成功!',false,800);
						var funStr = "window.frames['" + mFrameName + "']."+ mDialogCallback;
						eval("(" + funStr + ")");
						closeDataDialog();
					}else {
						closeDataDialog();
						showVelidateModal("操作失败!");
						return;
					}
				}
			});
		}
		if(action=='save'){
			if(!checkparam()){
				return;
			}
			// 保存运政机构
			$.ajax({
				type : 'post',
				url : appCtx + "base/PubOrganzation/addPubOrganzation.html",
				data : $("#vehicleForm").serialize(),
				cache : false,
				dataType : "JSON",
				success : function(data) {
					if (data.code == "0") {
						global.ajaxLoading.pop(data.msg,false,2000);
						var funStr = "window.frames['" + mFrameName + "']."+ mDialogCallback;
						eval("(" + funStr + ")");
						closeDataDialog();
					}else {
						global.ajaxLoading.pop(data.msg,false,2000);
						return;
					}
				}
			});
		}
	}
};
Organzation.init();
/**
 * 检查新增运政机构参数
 */
function checkparam(){
	var area_id = $("input[name=area_id]").val();
	var orgnCode = $("#orgnCode").val();
	var orgnName = $("#orgnName").val();
	var contacts = $("#contacts").val();
	var email = $("#email").val();
	var contactsNumber = $("#contactsNumber").val();
	var contactsAddress = $("#contactsAddress").val();

	if(orgnCode==null||orgnCode==''){
		global.ajaxLoading.pop('请填写组织机构编码',false,1000);
		return false;
	}
	if(orgnName==null||orgnName==''){
		global.ajaxLoading.pop('请填写组织机构名称',false,1000);
		return false;
	}
	if(orgnName.indexOf(" ") >= 0){
		global.ajaxLoading.pop('组织机构名称不能包含空格',false,1000);
		return false;
	}
	if(contacts==null||contacts==''){
		global.ajaxLoading.pop('请填写联系人',false,1000);
		return false;
	}
	if(contactsNumber==null||contactsNumber==''){
		global.ajaxLoading.pop('请填写联系人号码',false,1000);
		return false;
	}else{
	    if(!(/^1[34578]\d{9}$/.test(contactsNumber))){
	    	global.ajaxLoading.pop('联系人号码格式错误',false,1000);
	        return false; 
	    }
	}
	if(email==null || email==''){
		global.ajaxLoading.pop('请填写邮箱地址',false,1000);
		return false;
	}
	if(contactsAddress==null||contactsAddress==''){
		global.ajaxLoading.pop('请填写联系人地址',false,1000);
		return false;
	}
	if(area_id===null||area_id==''){
		global.ajaxLoading.pop('请填选择所属区域',false,1000);
		return false;
	}
	return true;
}
